package com.qf.web.servlet;

import com.qf.domain.Goods;
import com.qf.domain.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/*
 *                             _ooOoo_
 *                            o8888888o
 *                            88" . "88
 *                            (| -_- |)
 *                            O\  =  /O
 *                         ____/`---'\____
 *                       .'  \\|     |//  `.
 *                      /  \\|||  :  |||//  \
 *                     /  _||||| -:- |||||-  \
 *                     |   | \\\  -  /// |   |
 *                     | \_|  ''\---/''  |   |
 *                     \  .-\__  `-`  ___/-. /
 *                   ___`. .'  /--.--\  `. . __
 *                ."" '<  `.___\_<|>_/___.'  >'"".
 *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *               \  \ `-.   \_ __\ /__ _/   .-` /  /
 *          ======`-.____`-.___\_____/___.-`____.-'======
 *                             `=---='
 *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                     佛祖保佑        永无BUG
 *            佛曰:
 *                   写字楼里写字间，写字间里程序员；
 *                   程序人员写程序，又拿程序换酒钱。
 *                   酒醒只在网上坐，酒醉还来网下眠；
 *                   酒醉酒醒日复日，网上网下年复年。
 *                   但愿老死电脑间，不愿鞠躬老板前；
 *                   奔驰宝马贵者趣，公交自行程序员。
 *                   别人笑我忒疯癫，我笑自己命太贱；
 *                   不见满街漂亮妹，哪个归得程序员？
 */
@WebServlet(value = "/goodsservlet")
public class GoodsServlet extends BaseServlet {
    //分页查询
    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //获取分页查询
        try {
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("pageSize");
            int page_num=1;
            int page_size=8;
            if(pageNum!=null&&!pageNum.trim().equals("")){
                page_num=Integer.parseInt(pageNum);
                if(page_num<1) {
                    page_num = 1;
                }
            }
            if(pageSize!=null&&!pageSize.trim().equals("")){
                page_size=Integer.parseInt(pageSize);
                if(page_size<1) {
                    page_num = 8;
                }
            }
            //2获取条件
            String typeId = request.getParameter("typeId");
            String name = request.getParameter("name");
            ArrayList<Object> params=new ArrayList<>();
            String where=" where 1=1";    //select * from tb_goods where typeid=1 and name like 'java' order by id;
            if(!StringUtils.empty(typeId)){
                where+=" and typeid=?";
                params.add(typeId);
            }
            if(!StringUtils.empty(name)){
                where+=" and name like ?";
                params.add(name);
            }
            GoodsService goodsService=new GoodsServiceImpl();
            PageBean<Goods> pageBean=goodsService.findPage(page_num,page_size,where,params);

            request.setAttribute("pageBean", pageBean);
            request.setAttribute("typeId", typeId);
            return "/goodsList.jsp";
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询失败");

            return "/message.jsp";
        }
    }


    //商品详情
    public String getGoodsById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String id = request.getParameter("id");
        if(StringUtils.empty(id)){
            request.setAttribute("msg", "商品id为空");
            return "/message.jsp";
        }
        GoodsService goodsService=new GoodsServiceImpl();
        try {
            int goodId = Integer.parseInt(id);
            Goods goods=goodsService.findById(goodId);
            request.setAttribute("goods", goods);
            return "/goodsDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询商品失败");
            return "/message.jsp";
        }

    }
}
